package com.heimo.agentlicence.ui.adaptor;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.heimo.agentlicence.R;
import com.heimo.agentlicence.model.AgentRecord;

public class ListItemAdaptor extends BaseAdapter {
	private Context context;
	private List<AgentRecord> dataList;
	private int selectedPos = -1;
	
	public ListItemAdaptor(Context context, List<AgentRecord> bookingList) {
		this.context = context;
		this.dataList = bookingList;
	}
	
	public void setBookingList(List<AgentRecord> dlist) {
		this.dataList = dlist;
	}
	
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		LayoutInflater mInflater = (LayoutInflater) context
				.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.list_item,
					null);
			holder = new ViewHolder();
			holder.imageView = (ImageView) convertView.findViewById(R.id.icon);
			holder.txtTitle = (TextView) convertView.findViewById(R.id.title);
			holder.txtBody = (TextView) convertView.findViewById(R.id.body);
			holder.txtRemark = (TextView) convertView.findViewById(R.id.remark);
			fnSetSelectedColor(convertView,position);
			convertView.setTag(holder);
		} else {
			fnSetSelectedColor(convertView,position);
			holder = (ViewHolder) convertView.getTag();
		}

		AgentRecord item = (AgentRecord) getItem(position);

		holder.txtTitle.setText(item.getName());
		// holder.txtTitle.setTypeface(null, Typeface.BOLD);
		
		holder.txtBody.setText(item.getLicence());
		holder.txtRemark.setText(item.getIssue_date()+" - "+item.getEnd_date());
//		holder.imageView.setImageResource(item.getImageID());
		return convertView;
	}

	private void fnSetSelectedColor(View convertView, int position) {
		if(selectedPos == position){
			convertView.setBackgroundColor(Color.CYAN);
        }else{
        	convertView.setBackgroundColor(Color.WHITE);
        }
	}

	public long getItemId(int position) {
		return position;
	}

	public Object getItem(int position) {
		return dataList.get(position);
	}

	public int getCount() {
		return dataList.size();
	}

	/* private view holder class */
	private class ViewHolder {
		ImageView imageView;
		TextView txtTitle;
		TextView txtBody;
		TextView txtRemark;
	}
	
	public AgentRecord getSelectedItem(){
		if(selectedPos>=0){
			AgentRecord b = (AgentRecord) getItem(selectedPos);
			return b;			
		}
		return null;
	}
	
	public int getSelectedIndex(){
		return selectedPos;
	}
	
	public void setSelectedPosition(int pos){
		selectedPos = pos;
		notifyDataSetChanged();
	}
	
	public void addItem(AgentRecord li){
		this.dataList.add(li);
		notifyDataSetChanged();
	}
	
	public void addItems(AgentRecord li){
		this.dataList.add(li);
	}
	
	public void update(){
		notifyDataSetChanged();
	}
	
	public void removeAll(){
		while(dataList.size()>0){
			dataList.remove(0);
		}
		notifyDataSetChanged();
	}
}